package com.zjitc.controller.admin;

import com.github.pagehelper.PageInfo;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

import java.util.List;

/**
 * Create by IntelliJ IDEA
 *
 * @author: lhl
 * @date-Time: 2018/4/10 0010
 * @description:
 */
@Controller
@RequestMapping(value = "/admin/user")
public class AdminUserControl {

    @Autowired
    private UserService service;

    @RequestMapping(value = "findUsers")
    public ModelAndView findAllUser(
            @RequestParam(name = "pageNum", defaultValue = "1", required = false) int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "4", required = false) int pageSize) {
        ModelAndView mv = new ModelAndView(new InternalResourceView("/admin/user/list.jsp"));
        List<User> users = service.findAll(pageNum, pageSize);
        PageInfo info = new PageInfo(users);
        mv.addObject("page", info);
        return mv;
    }

    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public String deleteUser(String uid) {
        service.deleteById(uid);
        return uid;
    }

    @RequestMapping(value = "/findUserByUid")
    @ResponseBody
    public ModelAndView findUserByUid(String uid) {
        User user = service.findByUid(uid);
        ModelAndView mv = new ModelAndView(new InternalResourceView("/admin/user/edit.jsp"));
        mv.addObject("user", user);
        return mv;
    }

    @RequestMapping(value = "/updateUser")
    public String updateUser(User user) {
        service.update(user);
        return "redirect:/admin/user/findUsers.do";
    }
}
